<?php

namespace common\modules\wechat\models\backend;

use common\modules\wechat\widgets\ReplyKeywordDynamicForm;
use common\widgets\ActiveForm;
use common\widgets\Form;
use common\widgets\Select2;
use Yii;
use yii\helpers\ArrayHelper;

class ReplyRuleForm extends ReplyRule
{

    const SCENARIO_CREATE = 'admin-create';
    const SCENARIO_UPDATE = 'admin-update';

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return [
            'admin-create' => array_keys($this->attributeLabels()),
            'admin-update' => array_keys($this->attributeLabels()),
        ];
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return parent::rules();
    }

    /**
     * 返回当前表单的名称
     *
     * @return string
     */
    public function classLabelName()
    {
        return Yii::t('backend/wechat', 'Reply Rule Form');
    }

    /**
     * 列表定义
     *
     * @param ActiveForm|Form $form
     * @param static  $model
     * @param mixed  $extra
     * @return array
     * @throws \yii\web\ServerErrorHttpException
     */
    public function formRows(&$form, $model, $extra)
    {
        return [
            [
                'columns'    => 3,
                'attributes' => [
                    'id' => [
                        'type'    => Form::INPUT_TEXT,
                        'options' => ['disabled' => true],
                    ],
                    'mid' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => Select2::className(),
                        'options'     => [
                            'pluginOptions' => [
                                'width' => '100%',
                            ],
                            'data' => ArrayHelper::map(Module::find()->all(), 'id', 'name'),
                        ],
                    ],
                    'name' => [
                        'type'    => Form::INPUT_TEXT,
                    ],
                    'status' => [
                        'type'        => Form::INPUT_WIDGET,
                        'widgetClass' => Select2::className(),
                        'options'     => [
                            'pluginOptions' => [
                                'width' => '100%',
                            ],
                            'data' => self::$statuses,
                        ],
                    ],
                    'processor' => [
                        'type'    => Form::INPUT_TEXT,
                        'options' => ['maxlength' => true, 'placeholder' => self::PROCESSOR_DEFAULT],
                    ],
                    'priority' => [
                        'type'    => Form::INPUT_TEXT,
                        'options' => ['maxlength' => true, 'placeholder' => 0],
                    ],
                ],
            ],
            [
                'columns'    => 1,
                'attributes' => [
                    'keywords' => [
                        'type'    => Form::INPUT_WIDGET,
                        'widgetClass' => ReplyKeywordDynamicForm::className(),
                        'label' => '',
                        'options' => [
                            'form' => $form,
                            'model' => $model,
                            'formOptions' => [
                                'limit' => 999,
                                'model' => $model,
                                'formId' => $form->id,
                            ],
                        ],
                    ],
                ],
            ],
            /*
            [
                'columns'    => 4,
                'attributes' => [
                    'created_at' => [
                        'type'        => Form::INPUT_STATIC,
                    ],
                    'updated_at' => [
                        'type'        => Form::INPUT_STATIC,
                    ],

                    'created_by' => [
                        'type' => Form::INPUT_STATIC,
                    ],
                    'updated_by' => [
                        'type' => Form::INPUT_STATIC,
                    ],
                ],
            ],
            */
        ];
    }
}
